package com.example.fragmenttest.Fragment;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

import com.example.fragmenttest.Adapter.UserAdapter;
import com.example.fragmenttest.Bean.User;
import com.example.fragmenttest.MainActivity;
import com.example.fragmenttest.R;

import org.litepal.LitePal;

import java.util.ArrayList;
import java.util.List;

public class FirstFragment extends Fragment {
    private ListView lv ; // 1.声明变量
    //    private List<String> dataList =new ArrayList<>();删除了
    private UserAdapter adapter ;
    private  List<User> userList =new ArrayList<>() ;
    private int permission =0 ;

    // String[] fruits={"苹果","香蕉","橘子","山竹","榴莲"};  //2.添加数据源

    public FirstFragment() {
        // Required empty public constructor
    }
    @Override   //onCreate执行
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view =inflater.inflate(R.layout.fragment_first, container, false);
        initView(view);
        setListener();
        return  view ;
    }
    private void setListener() {


        SharedPreferences sp=getActivity().getSharedPreferences("userinfo",Context.MODE_PRIVATE);
        permission =sp.getInt("permission",-1);

        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() { //setOnItemClickListener
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //点击列表弹窗
                switch (permission){
                    case 1:
                        showToast("你点击了"+userList.get(position).getName());
                        break;
                    case 2:
                        if (permission==userList.get(position).getPermission()){
                            showToast("不可以对管理员进行操作");
                        }else {
                            showDiolog(userList.get(position));
                            //调用弹窗方法
                        }
                        break;

                }
            }
        });
    }
    /*
    对话框弹窗
    * */
    private  void showDiolog(final User user){
        AlertDialog.Builder builder=new AlertDialog.Builder(getContext());
        builder.setIcon(R.mipmap.leimua);//创建图片
        builder.setTitle("删除用户");//创建标题
        builder.setMessage("是否删除该用户");//创建弹窗消息
        builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                LitePal.delete(User.class,user.getId());//
                // 删除数据库中的ID
                userList.remove(user);
                adapter.notifyDataSetChanged();
                //更新
                showToast("已经删除用户");
                /*点击删除后的实际删除操作*/

            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                showToast("你点击了取消");
            }
        });

        builder.create().show();//创建并且尝试
    }
    private  void showToast( String str){
        Toast.makeText(getContext(),str,Toast.LENGTH_SHORT).show();
    }
    private void initView(  View view) {
        userList = LitePal.findAll(User.class);
//        dataList.clear();//清空数据源
//        for (User u:userList){
//            dataList.add("id:"+u.getId()+"    名字："+u.getName()+"  密码："+u.getPassword());
//        }
        lv=view.findViewById(R.id.lv );
        adapter=new UserAdapter(getContext(),R.layout.user_list,userList);
        // 1. 环境 2.   子项布局 3.  数据源
        //   ArrayAdapter<String> adapter=new ArrayAdapter<String>(getContext(),android.R.layout.simple_list_item_1,dataList);
        /*因为是碎片所以不能直接用MainActity，要用getContext*/
        lv.setAdapter(adapter);

    }

}